package pers.cz.jefconfig.config;

import pers.cz.jefconfig.jmx.JefMonitorRegister;
import pers.cz.jefconfig.tools.UnsafeUtils;
import sun.misc.Unsafe;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: JefConfig
 * @description: 自定义配置类
 * @author: Cheng Zhi
 * @create: 2022-07-14 11:07
 **/
public class JefConfiguration {

    private static String fileName = "jef.properties";

    private static File file;

    private static Configuration configuration;

    static {
        if (configuration == null) {
            // JVM启动参数中添加-Djef.properties=xxx.properties 来指定自己的配置文件或者System.setProperty()来设置
            String property = System.getProperty("jef.properties");

            if (property == null) {
                property = fileName;
            }
            configuration = new Configuration(property);
        }

        JefMonitorRegister.registeJefDefault();
    }

    public static double getDouble(ConfigItem configItem, int defaultValue) {

        return configuration.getDouble(configItem, defaultValue);
    }

    public static long getLong(ConfigItem configItem, long defaultValue) {

        return configuration.getLong(configItem, defaultValue);
    }

    public static boolean getBoolean(ConfigItem configItem, boolean defaultValue) {

        return configuration.getBoolean(configItem, defaultValue);
    }

    public static int getInt(ConfigItem configItem, int defaultValue) {

        return configuration.getInt(configItem, defaultValue);
    }

    public static String get(ConfigItem configItem) {

        String key = configItem.toString().toLowerCase();
        return String.valueOf(configuration.get(key, ""));
    }

    public static String get(ConfigItem configItem, String defalutKeyValue) {
        String key = configItem.toString().toLowerCase();
        return String.valueOf(configuration.get(key, defalutKeyValue));
    }

    public static void set(ConfigItem configItem, Object value) {
        configuration.set(configItem, value);
    }

    public static void set(String key, Object value) {
        configuration.set(key, value);
    }

    public static Object getObj(ConfigItem configItem) {
        return configuration.getObj(configItem);
    }

    public static <T> T getObj(ConfigItem configItem, Class<T> clazz) {
        Map map = (Map) configuration.getObj(configItem);
        return UnsafeUtils.mapToEntity(map, clazz);
    }

    /**
     * 获取为一个LIST对象
     * @param configItem
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> getList(ConfigItem configItem, Class<T> clazz) {
        List list = new ArrayList();
        List<Map> innerList =configuration.getObj(configItem, List.class);
        for (Map map : innerList) {
            T t = UnsafeUtils.mapToEntity(map, clazz);
            list.add(t);
        }
        return list;
    }

    public static Map<String, Object> getAllCache() {
        return configuration.getAllCache();
    }
}
